package com.invengo.module_check.ui.fragment.opeartion.item;

import static com.invengo.library_db.entity.module_check.BookState.ERROR_NO_LISTING;
import static com.invengo.library_db.entity.module_check.BookState.ERROR_OTHER_SHELF;
import static com.invengo.library_db.entity.module_check.BookState.IN_SHELF;
import static com.invengo.library_db.entity.module_check.BookState.LENG_OUT;
import static com.invengo.library_db.entity.module_check.BookState.LENG_OUT_IN_SHELF;
import static com.invengo.library_db.entity.module_check.BookState.NEED_IN_SHELF;

import android.annotation.SuppressLint;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.invengo.library_db.entity.module_check.BookBean;
import com.invengo.module_check.R;
import com.invengo.module_check.ui.fragment.opeartion.OpeartionInfoViewModel;

import java8.util.Optional;

import me.goldze.mvvmhabit.base.ItemViewModel;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.utils.KLog;
import me.goldze.mvvmhabit.utils.StringUtils;

public class OpBookInfoItemViewModel extends ItemViewModel<OpeartionInfoViewModel> {

    public SingleLiveEvent<BookBean> book = new SingleLiveEvent<>();
    public SingleLiveEvent<String> shelfName = new SingleLiveEvent<>();
    public SingleLiveEvent<String> tipContent = new SingleLiveEvent<>();
    public SingleLiveEvent<Integer> tipColor = new SingleLiveEvent<>();

    public OpBookInfoItemViewModel(@NonNull OpeartionInfoViewModel viewModel, BookBean bean,  String oldShelfName) {
        super(viewModel);
        this.book.postValue(bean);
        if (!TextUtils.isEmpty(oldShelfName)) {
            this.shelfName.postValue("原架位号：" + bean.getOldShelfName());
        } else {
            this.shelfName.postValue("架位号：" + Optional.ofNullable(bean.getShelfName()).orElse(""));
        }
        if (!TextUtils.isEmpty(bean.getTip()))
            updateState(bean);
    }

    public void updateState(BookBean bean) {
        book.postValue(bean);
        switch (bean.getTip()) {
            case ERROR_OTHER_SHELF:
                tipColor.postValue(viewModel.getApplication().getColor(R.color.color_red));
//                tipContent.postValue(bean.getTip() );
                tipContent.postValue(bean.getTip() + "\n" + bean.getShelfName());
                break;
            case ERROR_NO_LISTING:
                tipColor.postValue(viewModel.getApplication().getColor(R.color.color_red));
                tipContent.postValue(bean.getTip());
                break;
            case LENG_OUT_IN_SHELF:
                tipColor.postValue(viewModel.getApplication().getColor(R.color.color_red));
                tipContent.postValue(bean.getTip() + "\n 请取出");
                break;
            case LENG_OUT:
                tipColor.postValue(viewModel.getApplication().getColor(R.color.color_orange));
                tipContent.postValue(bean.getTip() + (StringUtils.isEmpty(bean.getNeedBackDate()) ? "，请取出" : (" 应归日期：" + bean.getNeedBackDate())));
                break;
            case IN_SHELF:
                tipColor.postValue(viewModel.getApplication().getColor(R.color.color_green));
                tipContent.postValue(bean.getTip());
                break;
            case NEED_IN_SHELF:
                tipColor.postValue(viewModel.getApplication().getColor(R.color.color_blue));
                tipContent.postValue("[上架未找到]");
                break;
            default:
                KLog.e(">>>  " + bean.toString());
                break;
        }
    }

    public int getPosition() {
        return viewModel.getPosition(this) + 1;
    }
}
